
using System;
using System.Linq;
using Csla;
using Csla.Data;
using Csla.Security;
using Csla.Validation;
using HousingInfo.DataLinQ;

namespace HousingInfo.Library
{ 
	public partial class PriceAdvertisementBO : Csla.BusinessBase<PriceAdvertisementBO>
	{

		#region Authorization Rules
		protected override void AddAuthorizationRules()
		{
			//TODO: Define authorization rules in PriceAdvertisementBO
			//AuthorizationRules.AllowRead(PriceAdvertisementIDProperty, "PriceAdvertisementBOReadGroup");
			//AuthorizationRules.AllowRead(PriceAdvertisementNameProperty, "PriceAdvertisementBOReadGroup");
			//AuthorizationRules.AllowRead(PriceProperty, "PriceAdvertisementBOReadGroup");

			//AuthorizationRules.AllowWrite(PriceAdvertisementNameProperty, "PriceAdvertisementBOWriteGroup");
			//AuthorizationRules.AllowWrite(PriceProperty, "PriceAdvertisementBOWriteGroup");
		}

		protected static void AddObjectAuthorizationRules()
		{
			//TODO: Define object-level authorization rules of PriceAdvertisementBO
			//AuthorizationRules.AllowCreate(typeof(PriceAdvertisementBO), "PriceAdvertisementBOCreateGroup"));
			//AuthorizationRules.AllowEdit(typeof(PriceAdvertisementBO), "PriceAdvertisementBOEditGroup"));
			//AuthorizationRules.AllowDelete(typeof(PriceAdvertisementBO), "PriceAdvertisementBODeleteGroup"));
		}
		#endregion //Authorization Rules
		private void AssignParentKey(HousingInfo.DataLinQ.PriceAdvertisement data)
		{
			 //data.ParentId = ReadProperty<ParentBO>(ParentBOProperty).Id;
		}

        #region Kiem tra ve max - min cua gia quang cao
        public static int checkvip_newshouse(string maxmin)
        {
            using (var mgr = ContextManager<HousingInfoDataContext>
                    .GetManager(Database.HousingInfoConnection))
            {
                int returnmaxmin = 0;
                try
                {
                    if (maxmin == "max")
                    {
                        returnmaxmin = (from row in mgr.DataContext.PriceAdvertisements
                                   orderby row.Price descending
                                   select (row.Price)).Max();
                        
                    }
                    else
                    {
                        returnmaxmin = (from row in mgr.DataContext.PriceAdvertisements
                                        orderby row.Price ascending
                                        select (row.Price)).Min();
                    }
                    return returnmaxmin;
                }
                catch
                {
                    return returnmaxmin;//tra ve 0 neu khong phai tin vip
                }
            }
        }
        #endregion
	}
}
